import { _decorator, Component, Node, PhysicsSystem2D, Sprite, v2, Toggle, Vec3, UITransform, RichText, AudioSource } from 'cc';
import { CreateImgs } from './CreateImgs';

const { ccclass, property } = _decorator;

@ccclass('GameManage')
export class GameManage extends Component {
    @property(UITransform)
    public canvas:UITransform=null;
    @property(Node)
    gameOver:Node=null;
    @property(RichText)
    scoreNode:RichText=null;
    @property(AudioSource)
    disbaleMusic:AudioSource=null;
    @property(AudioSource)
    bgm:AudioSource=null;
    @property(AudioSource)
    errorMusic:AudioSource=null;
    @property(Node)
    changes:Node=null;
    @property(CreateImgs)
    createImgs:CreateImgs=null;
    @property(Node)
    muteButton:Node=null;
    @property(Node)
    audioManage:Node=null;
    start() {
        //获取宽高
        this.height=this.canvas.height;
        this.width=this.canvas.width;
    }
    //匹配成功就禁用
    checkPic(e: Node) {
        if(!this.delth)
        {
            this.delth=e.getComponent(UITransform).height*e.scale.y;
            this.deltw=e.getComponent(UITransform).width*e.scale.x;
        }
        e===this.arr[0]?this.arr.shift():this.arr.push(e);
        if (this.arr.length >= 2) {
            //判断选的两张图片是否相同
            if (this.arr[0].getComponent(Sprite).spriteFrame.name != this.arr[1].getComponent(Sprite).spriteFrame.name) {
                this.errorMusic.play();
                    let t =setTimeout(() => {
                        this.arr[0].getComponent(Toggle).setIsCheckedWithoutNotify(false);
                        this.arr[1].getComponent(Toggle).setIsCheckedWithoutNotify(false);
                        this.arr=new Array();
                        clearTimeout(t);
                    },this.time)
            }
            else if (this.arr[0] !== this.arr[1]) {
                //判断逻辑
                let flag = false;
                if(this.arr[0].getWorldPosition().x>this.arr[1].getWorldPosition().x)
                    flag = this.judgePic(this.arr[0].getWorldPosition(), this.arr[1].getWorldPosition(),false);
                    else
                    flag = this.judgePic(this.arr[1].getWorldPosition(), this.arr[0].getWorldPosition(),true);
                if (flag) {
                    let t=setTimeout(() => {
                        this.audioManage.active?this.disbaleMusic.play():null;
                        this.arr[0].active=false;
                        this.arr[1].active=false;
                        this.arr=new Array();
                        this.picNumber-=2;
                        this.score++;
                        this.scoreNode.string=this.scoreNode.string.replace(/>\d+</,`>${this.score}<`);
                        if(this.picNumber==0)
                        {
                            this.bgm.stop();
                            this.gameOver.active=true;
                            this.changes.active=false;
                            this.muteButton.active=false;
                            this.createImgs.arr=new Array();
                        }
                        clearTimeout(t);
                    },this.time)
                }
                else {
                    
                    this.audioManage.active?this.errorMusic.play():null;
                    let t=setTimeout(() => {
                        this.arr[0].getComponent(Toggle).setIsCheckedWithoutNotify(false);
                        this.arr[1].getComponent(Toggle).setIsCheckedWithoutNotify(false);
                        this.arr=new Array();
                        clearTimeout(t);
                    },this.time)
                }
            }
                
        }
    }
    //配对规则
    judgePic(one: Vec3, two: Vec3,c:boolean): boolean {
        //判断是否相邻
        if (PhysicsSystem2D.instance.raycast(v2(one.x, one.y), v2(two.x, two.y))[0].collider.node===(c?this.arr[0]:this.arr[1]))
        {
            if(one.x===two.x||one.y===two.y)
            return true;
        }
        this.arr1[0] = PhysicsSystem2D.instance.raycast(v2(one.x, one.y), v2(one.x, 0)).length;
        this.arr1[0] += PhysicsSystem2D.instance.raycast(v2(two.x, two.y), v2(two.x, 0)).length;
        this.arr1[1] = PhysicsSystem2D.instance.raycast(v2(one.x, one.y), v2(one.x, this.height)).length;
        this.arr1[1] += PhysicsSystem2D.instance.raycast(v2(two.x, two.y), v2(two.x, this.height)).length;
        this.arr1[2] = PhysicsSystem2D.instance.raycast(v2(one.x, one.y), v2(0, one.y)).length;
        this.arr1[2] += PhysicsSystem2D.instance.raycast(v2(two.x, two.y), v2(0, two.y)).length;
        this.arr1[3] = PhysicsSystem2D.instance.raycast(v2(one.x, one.y), v2(this.width, one.y)).length;
        this.arr1[3] += PhysicsSystem2D.instance.raycast(v2(two.x, two.y), v2(this.width, two.y)).length;
        //判断同方向是否能相通
        if (this.arr1[0] && this.arr1[1] && this.arr1[2] && this.arr1[3]) {
            //判断左边方向
            for (let i = 1; one.x - i * this.deltw > 0; i++) {
                this.arr1[4] = PhysicsSystem2D.instance.raycast(v2(one.x, one.y), v2(one.x - i * this.deltw, one.y)).length;
                this.arr1[4] += PhysicsSystem2D.instance.raycast(v2(two.x, two.y), v2(one.x - i * this.deltw, two.y)).length;

                if (!this.arr1[4]) {
                    this.arr1[4] += PhysicsSystem2D.instance.raycast(v2(one.x - i * this.deltw, two.y),v2(one.x - i * this.deltw, one.y)).length;
                    // console.log('this.arr1[4] 左 :>> ', this.arr1[4]);
                    if (!this.arr1[4])
                        return true;
                }
            }
            //判断右边方向
            for (let i = 1; one.x + i * this.deltw < this.width; i++) {
                this.arr1[4] = PhysicsSystem2D.instance.raycast(v2(one.x, one.y), v2(one.x + i * this.deltw, one.y)).length;
                this.arr1[4] += PhysicsSystem2D.instance.raycast(v2(two.x, two.y), v2(one.x + i * this.deltw, two.y)).length;
                if (!this.arr1[4]) {
                    this.arr1[4] += PhysicsSystem2D.instance.raycast(v2(one.x + i * this.deltw, two.y),v2(one.x + i * this.deltw, one.y)).length;
                    // console.log('this.arr1[4] 右 :>> ', this.arr1[4]);
                    if (!this.arr1[4])
                        return true;
                }
            }
            //y高,下
                for (let i = 1; one.y - i * this.delth > 0; i++) {
                    this.arr1[4] = PhysicsSystem2D.instance.raycast(v2(one.x, one.y), v2(one.x, one.y - i * this.delth)).length;
                    this.arr1[4] += PhysicsSystem2D.instance.raycast(v2(two.x, two.y), v2(two.x, one.y - i * this.delth)).length;
                    if (!this.arr1[4]) {
                        this.arr1[4] += PhysicsSystem2D.instance.raycast(v2(two.x, one.y - i * this.delth),v2(one.x, one.y - i * this.delth) ).length;
                        // console.log('this.arr1[4] 下:>> ', this.arr1[4]);
                        if (!this.arr1[4])
                            return true;
                    }
                }
            //y低,上
                for (let i = 1; one.y + i * this.delth < this.height; i++) {
                    this.arr1[4] = PhysicsSystem2D.instance.raycast(v2(one.x, one.y), v2(one.x, one.y + i * this.delth)).length;
                    this.arr1[4] += PhysicsSystem2D.instance.raycast(v2(two.x, two.y), v2(two.x, one.y + i * this.delth)).length;
                    if (!this.arr1[4]) {
                        this.arr1[4] += PhysicsSystem2D.instance.raycast(v2(two.x, one.y + i * this.delth),v2(one.x, one.y + i * this.delth)).length;
                        // console.log('this.arr1[4] 上:>> ', this.arr1[4]);
                        if (!this.arr1[4])
                            return true;
                    }
                }
            
            return false;
        }
        else {
            return true
        }

    }

    private arr: Array<Node> = [];
    private deltw: number = null;
    private delth: number = null;
    private arr1: Array<number> = [];
    private height:number=0;
    private width:number=0;
    private time:number=200;
    public picNumber:number=null;
    public score:number=0;
}

